package com.dx.mes.service.brm.service;

import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_OK;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_SET_ERROR;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_MESG_OK;
import static com.dx.mes.common.bean.def.GenericDef.TRX_OUT;
import static com.dx.mes.common.bean.def.GenericDef.T_FPPMOVEIN;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dx.mes.common.dao.IRetPrdInfoDao;
import com.dx.mes.common.transcoder.util.AppContext;
import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.common.transcoder.util.LogUtils;
import com.dx.mes.facade.brm.service.IFbpbisdataService;
import com.dx.mes.facade.brm.service.IFbpmesbcService;
import com.dx.mes.facade.brm.tx.Fbpmesbc.FbpmesbcI;
import com.dx.mes.facade.brm.tx.Fbpmesbc.FbpmesbcO;

@Service("fbpmesbcService")
public class FbpmesbcService implements IFbpmesbcService{
	private static final String FBPMESBC = "FBPMESBC";

	private LogUtils logUtils;
	
	@Autowired
	private IRetPrdInfoDao retPrdInfoDao;
	@Override
	public String subMainProc(String evt_no, String strInMsg) {
		AppContext.clear();
		AppContext.setCurrServiceName(FbpmesbcService.class.getSimpleName());
		AppContext.setCurrEventNumber(evt_no);
		logUtils = new LogUtils(FbpmesbcService.class);
		logUtils.info("[InTrx:" + strInMsg + "]");
		
		String strOutMsg = null;
		FbpmesbcI inTrx = new FbpmesbcI();
		FbpmesbcO outTrx = new FbpmesbcO();
		
		outTrx.setTrx_id(T_FPPMOVEIN);
		outTrx.setType_id(TRX_OUT);
		outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));
		try {
			inTrx = JacksonUtil.fromJson(strInMsg, FbpmesbcI.class);
			
			outTrx = subMainProc2(inTrx);
			if (RETURN_CODE_OK.equals(outTrx.getRtn_code())) {
				retPrdInfoDao.flush();
			} else {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		} catch (Exception ex) {
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			outTrx.setRtn_mesg(ex.getCause().toString());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		} finally {
			strOutMsg = JacksonUtil.toJSONStr(outTrx);
			logUtils.info("[OutTrx:" + strOutMsg + "]");
		}
		return strOutMsg;
	}
	private FbpmesbcO subMainProc2(FbpmesbcI inTrx) {
		FbpmesbcO outTrx = new FbpmesbcO();
		outTrx.setRtn_code(RETURN_CODE_OK);
		return outTrx;
	}
	
}
